package com.skyward.android.androiddemo2.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by skyward on 2017/3/20.
 * email:
 */

public class TestView extends ViewGroup {


    public TestView(Context context) {
        super(context);
    }

    public TestView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TestView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

//        Log.i("myLog","widthMode: "+widthMode+" heightMode: "+heightMode);
//        Log.i("myLog","widthSize: "+widthSize+" heightSize: "+heightSize);

        int pl = getPaddingLeft();
        int pt = getPaddingTop();
        int pr = getPaddingRight();
        int pb = getPaddingBottom();
        // 测量所有子 View，当测量后才能获取到子 View 的测量宽高
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int width =0;
        int height = 0;

        if(widthMode == MeasureSpec.AT_MOST){
            int maxWidth = 0;
            for (int i = 0; i < getChildCount(); i++) {
                View childAt = getChildAt(i);
                if(childAt.getVisibility() == GONE){
                    continue;
                }
                MarginLayoutParams lp = (MarginLayoutParams) childAt.getLayoutParams();
                int childWidth = childAt.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
                 maxWidth = childWidth > maxWidth ? childWidth : maxWidth;
            }
            width = maxWidth + pl + pr;
        }else if(heightMode == MeasureSpec.AT_MOST){
            for (int i = 0; i < getChildCount(); i++) {
                View childAt = getChildAt(i);
                if(childAt.getVisibility() == GONE){
                    continue;
                }
                MarginLayoutParams lp = (MarginLayoutParams) childAt.getLayoutParams();
                height += childAt.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            }
            height = height + pt + pb;
        }

        setMeasuredDimension(widthMode == MeasureSpec.AT_MOST ? width : widthSize,heightMode == MeasureSpec.AT_MOST ? height:heightSize);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int pl = getPaddingLeft();
        int pt = getPaddingTop();
        int pr = getPaddingRight();
        int pb = getPaddingBottom();

        int cl = 0;
        int ct = 0;
        int cr = 0;
        int cb = 0;
        int bm = 0;

        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);

            MarginLayoutParams lp = (MarginLayoutParams) childAt.getLayoutParams();

            cl = lp.leftMargin;
            ct += lp.topMargin;
            cr = childAt.getMeasuredWidth() + lp.leftMargin;
            cb += childAt.getMeasuredHeight() + lp.topMargin;
            // 对子 View 进行布局
            childAt.layout(cl + pl, ct + pt + bm, cr + pr, cb + pb + bm);
            ct += childAt.getMeasuredHeight();
            bm += lp.bottomMargin;
        }

    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }
}
